package com.lai.note.util;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.lai.note.adapter.WenanRecyclerAdapter;
import com.lai.note.dto.ResponseResult;
import com.lai.note.model.Wenan;
import com.lai.note.service.WenanService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ListWenanUtil {

    public static void initDatas(Context context, WenanRecyclerAdapter adapter){
        List<Wenan> wenans = new ArrayList<Wenan>();
        Retrofit retrofit = RetrofitRequest.getInstance(context);
        WenanService wenanService = retrofit.create(WenanService.class);
        Call<ResponseResult> call = wenanService.lookCopyWritingDetails();
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if(res!=null && res.getStatus()==200){
                    String wenanJson = GsonUtil.object2Json(res.getData());
                    JsonParser parser = new JsonParser();
                    JsonArray jsonArray = parser.parse(wenanJson.toString()).getAsJsonArray();
                    System.out.println("jsonArray:-->"+jsonArray);
                    Gson gson = new Gson();
                    for(JsonElement wenan:jsonArray){
                        Wenan wenan1 = gson.fromJson(wenan,Wenan.class);
                        wenans.add(wenan1);
                    }
                    adapter.refresh(wenans);
                    ToastUtil.display(context,"^_^获取信息成功^_^");
                }else{
                    ToastUtil.display(context,"-_-获取失败-_-");
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
